package com.googlecode.imule.apps;

import info.clearthought.layout.TableLayout;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.jdesktop.animation.timing.Animator;
import org.jdesktop.animation.timing.interpolation.SplineInterpolator;
import org.jdesktop.animation.transitions.ScreenTransition;
import org.jdesktop.animation.transitions.TransitionTarget;
import org.jdesktop.swingx.JXButton;
import org.jdesktop.swingx.JXPanel;



/**
 * An simple iPhone-like menu system for the application
 * 
 * @author Neil Cowzer
 */
/* TODO: finalise implementation and Document
 * Modify to use the tablelayout, keep at top and not to expand icon/buttons
 * Make it just the panel handling itself, need a another class to control the
 * 'context/app' switch.
 * implement explode and implode.
 */
public class eMenu extends JXPanel implements TransitionTarget{

	private TableLayout layout;

	private List<iButtonPosition> list;	// list of buttons 

	private int count;	// number of buttons

	private JXPanel contentPanel, buttonPanel;	// temp panels

	private ScreenTransition st;

	public eMenu(){
		super();
		initComponent();

		// -- create the animator object (set delay and interpolator)
		Animator anim = new Animator(600);
		// http://javadesktop.org/swinglabs/demos/timingframework/SplineEditor.jnlp
		anim.setInterpolator(new SplineInterpolator(
				0.00f, 1.00f,
				1.00f, 0.98f));

		// -- setup the screen transition object, with TransitionTarget & Animator
		st = new ScreenTransition(
				contentPanel,
				this,
				anim
		);
	}

	public void add(iMenuButton button){
		iButtonPosition toAdd = new iButtonPosition(button, -1, -1);

		list.add(toAdd);

		sort();

		count++;
	}

	private void sort(){
		if (list.size()%3 == 0)
			list.get(list.size()-1).button.setName("a");
		
		Collections.sort(list);

		int i = 0;
		for (iButtonPosition data : list) {
			int x = data.x;
			int y = data.y;

			if (data.x==i%layout.getNumColumn() && data.y==i/layout.getNumColumn()) {
				System.out.println("correct");
			} else{
				data.x = i%layout.getNumColumn();
				data.y = i/layout.getNumColumn();

				if (data.y >= layout.getNumRow()){
					System.out.println("Add a row");
				} else {
					// TODO: use TableLayoutConstraints to center Object
					contentPanel.add(data.button, ""+data.x+", "+data.y+"");
					System.out.println("adding: ("+data.x+", "+data.y+")");
				}
			}
			//}
			i++;
			//}
		}
		System.out.println();
	}

	
	public void setupNextScreen() {
		//contentPanel.removeAll();
		contentPanel.setLayout(layout);


		if (count%2 == 0)
				add(new iMenuButton("/resources/Music.png", "x"));
		else
			add(new iMenuButton("/resources/Mail.png", "x"));



		contentPanel.revalidate();
		contentPanel.repaint();
	}


	private void setupOffScreen(){
		contentPanel.setLayout(null);

		iMenuButton button = new iMenuButton("/resources/Mail.png", "0");
			list.add(new iButtonPosition(button, 700, 20));
			contentPanel.add(button);
			Dimension size = button.getPreferredSize();
			button.setBounds(700, 20, size.width, size.height);
		 
		iMenuButton button1 = new iMenuButton("/resources/Mail.png", "1");
			list.add(new iButtonPosition(button1, 700, 800));
			contentPanel.add(button1);
			size = button1.getPreferredSize();
			button1.setBounds(700, 800, size.width, size.height);
			
			button1 = new iMenuButton("/resources/Music.png", "2");
			list.add(new iButtonPosition(button1, -700, -800));
			contentPanel.add(button1);
			size = button1.getPreferredSize();
			button1.setBounds(-700, -800, size.width, size.height);
			
			button1 = new iMenuButton("/resources/Music.png", "2");
			list.add(new iButtonPosition(button1, -90, -800));
			contentPanel.add(button1);
			size = button1.getPreferredSize();
			button1.setBounds(-90, -800, size.width, size.height);
	}



	/**
	 * Instantiate the components
	 */
	private void initComponent(){
		double f = TableLayout.FILL;

		double[][] size = {
				{f, f, f}, 
				{f, f, f, f}};
		layout = new TableLayout(size);
		//layout = new GridLayout(0,3);

		JXButton button = new JXButton("Add");
		button.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				transition(e);
			}
		});
		buttonPanel = new JXPanel();
		buttonPanel.add(button);

		contentPanel = new JXPanel();
		contentPanel.setLayout(layout);
		contentPanel.setBackground(Color.BLACK);

		setLayout(new BorderLayout());
		add(contentPanel, BorderLayout.CENTER);
		add(buttonPanel, BorderLayout.SOUTH);

		list = new ArrayList<iButtonPosition>();
		count = 0;

		setupOffScreen();
	}

	private void transition(ActionEvent e){
		st.start();
	}


	protected class iButtonPosition implements Comparable<iButtonPosition>{

		protected iMenuButton button;
		protected int x,y;

		public iButtonPosition(iMenuButton button, int x, int y) {
			this.button = button;
			this.x = x;
			this.y = y;
		}

		public int compareTo(iButtonPosition arg0) {
			return button.getName().compareTo(arg0.button.getName());
		}
	}
}
